﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Husb.Data;
using Husb.Util;
using GroundWater.Data;
using GroundWater.DataAccess;
using UserTable = GroundWater.Data.UserDataSet.UsersDataTable;
using UserRow = GroundWater.Data.UserDataSet.UsersRow;

namespace GroundWater.BusinessActions
{
    public class Users : DataAccessManager<UserDataSet, UserTable, UserRow, UserDataAdapter>
    {

        public static DataTable FetchMenusAll()
        {
            return UserDataAdapter.FetchMenusAll();
        }

        public static int InsertAuth(string userId, string menu)
        {
            return UserDataAdapter.InsertAuth(userId, menu);
        }

        public static int DeleteAuth(string userId, string menu)
        {
            return UserDataAdapter.DeleteAuth(userId, menu);
        }

        public static int DeleteAuthByUserId(string userId)
        {
            return UserDataAdapter.DeleteAuthByUserId(userId);
        }

        public static DataTable FetchAuth()
        {
            return UserDataAdapter.FetchAuth();
        }

        public static DataTable FetchAuthByUserId(string userId)
        {
            DataTable dt = UserDataAdapter.FetchAuth();

            DataTable tmp = dt.Clone();
            tmp.TableName = "UserAuthorization";
            if (dt == null || dt.Rows.Count == 0) 
                return tmp;
            foreach (DataRow row in dt.Rows)
            {
                if (row[0].Equals(userId))
                {
                    tmp.ImportRow(row);
                }
            }

            return tmp;
        }

        public static bool Authorize(string userId, string menu)
        {
            //bool authorize = false;
            DataTable dt = FetchAuthByUserId(userId, menu);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;

            //DataTable menus = FetchMenusAll();

            //foreach (DataRow row in dt.Rows)
            //{
            //    foreach (DataRow menu in menus.Rows)
            //    {
            //        if(menu["Id"].Equals(row["MenuId"]))
            //        {
            //            authorize = true;
            //        }
            //    }
            //}

            //return authorize;
        }

        public static DataTable FetchAuthByUserId(string userId, string menu)
        {
            DataTable dt = UserDataAdapter.FetchAuth();

            DataTable tmp = dt.Clone();
            tmp.TableName = "UserAuthorization";
            if (dt == null || dt.Rows.Count == 0)
                return tmp;
            foreach (DataRow row in dt.Rows)
            {
                if (row[0].ToString().ToLower() == userId.ToLower() && row[1].ToString() == menu) // row[0].Equals(userId) && row[1].Equals(menu)
                {
                    tmp.ImportRow(row);
                }
            }

            return tmp;
        }
    }

    public enum MenuIndex
    {

    }
}
